package cn.edu.gzhu.workOrderManagement.controller;

import cn.edu.gzhu.workOrderManagement.constants.MessageConstant;
import cn.edu.gzhu.workOrderManagement.pojo.dto.organization.OrganizationDeleteDto;

import cn.edu.gzhu.workOrderManagement.pojo.dto.organization.OrganizationListDto;
import cn.edu.gzhu.workOrderManagement.pojo.dto.organization.OrganizationRegisterDto;
import cn.edu.gzhu.workOrderManagement.pojo.dto.organization.OrganizationUpdateDto;
import cn.edu.gzhu.workOrderManagement.pojo.vo.OrganizationListVo;
import cn.edu.gzhu.workOrderManagement.result.Result;
import cn.edu.gzhu.workOrderManagement.service.OrganizationService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/organization")
@Tag(name = "组织管理接口")
public class OrganizationController {
    @Autowired
     OrganizationService organizationService;
    /**
     * 组织注册接口
     * @param organizationRegisterDto
     * @return
     */
    @Operation(summary = "组织注册")
    @PostMapping("/register")
    public Result register(@RequestHeader String token, @RequestBody @Validated OrganizationRegisterDto organizationRegisterDto) {


        Boolean isSuccess = organizationService.saveOrganization(organizationRegisterDto);
        if (!isSuccess){
            return Result.error(MessageConstant.PARAMETER_ERROR);
        }

        return Result.success();
    }

    /**
     * 组织更新接口
     * @param organizationUpdateDto
     * @return
     */
    @Operation(summary = "组织更新")
    @PostMapping("/update")
    public Result organizationUpdate(@RequestHeader String token, @RequestBody @Validated OrganizationUpdateDto organizationUpdateDto) {

        Boolean isSuccess = organizationService.updateOrganization(organizationUpdateDto);
        if (!isSuccess){
            return Result.error(MessageConstant.PARAMETER_ERROR);
        }

        return Result.success();
    }

    /**
     * 组织按条件分页查询
     *
     * @param organizationListDto
     * @return
     */
    @Operation(summary = "组织分页查询")
    @GetMapping("/list")
    public Result<IPage<OrganizationListVo>> OrganizationList(@RequestHeader String token, @Validated OrganizationListDto organizationListDto){

        IPage<OrganizationListVo> voIPage = organizationService.listOrganizationsByConditions(organizationListDto);
        return Result.success(voIPage);
    }

    /**
     * 按名称删除组织
     * @param organizationDeleteDto
     * @return
     */
    @Operation(summary = "按名称删除组织")
    @PostMapping("/delete")
    public Result organizationDelete(@RequestHeader String token, @RequestBody @Validated OrganizationDeleteDto organizationDeleteDto){

        Boolean isSuccess = organizationService.deleteOrganization(organizationDeleteDto);
        if (!isSuccess){
            return Result.error(MessageConstant.PARAMETER_ERROR);
        }
        return Result.success();
    }
}
